package cn.itsource.sys.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageList;
import cn.itsource.sys.domain.Dictionary;
import cn.itsource.sys.query.DictionaryQuery;
import cn.itsource.sys.service.IDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 数据字典
 */
@RestController
@RequestMapping("/dictionary")
public class DictionaryController {
    @Autowired
    private IDictionaryService dictionaryService;

    /** 添加和修改 */
    @PutMapping
    private AjaxResult addAndUpdate(@RequestBody Dictionary dictionary){
        //判断是否有id
        if (dictionary.getId() == null){ //没有id就是添加
            dictionaryService.add(dictionary);
        }else { //有id就是修改
            dictionaryService.update(dictionary);
        }
        return new AjaxResult();
    }
    /** 删除单个数据 */
    @DeleteMapping("/{id}")
    private AjaxResult delete(@PathVariable("id") Long id){
        dictionaryService.del(id);
        return new AjaxResult();
    }
    /** 批量删除 */
    @PatchMapping
    private AjaxResult patchDel(@RequestBody Long[] ids){
        dictionaryService.patchDel(ids);
        return new AjaxResult();
    }
    /** 查询单个数据 */
    @GetMapping("/{id}")
    private Dictionary findOne(@PathVariable("id") Long id){
        return dictionaryService.findOne(id);
    }
    /** 查询所有数据 */
    @GetMapping
    private List<Dictionary> findAll(){
        return dictionaryService.findAll();
    }
    /** 分页查询和高级查询 */
    @PostMapping
    private PageList<Dictionary> queryPage(@RequestBody DictionaryQuery dictionaryQuery){
        return dictionaryService.queryPage(dictionaryQuery);
    }
}
